Home

Computer science

'

C Programming: I\'m writing a program for a CRC 12. As of right now I only need help on my calculation portion. It\'s a program that takes in two argument values from the command line argv[1] is c or v (only foucused on c for now) and the second is argv[2] which is any uppercase hex value from 0-9 A-F 3 to 40 characters long. Im having a problem taking my known polynomial 1100 0101 1001 1 and dividing by the argv[2] input after appending it with 12 zeros (since its a CRC 12). I know the bit operator ^ is XOR but I don\'t know how to implement it into this array print out and the remainder print out. Here is what the output needs to look like (I have it up to "Number of zeroes that will be appended to the binary input: ")



HERE IS MY CODE I HAVE SO FAR:

//Libraries

#include

#include

#include

//Functional Prototypes

void modeType(char *argv);

void hexConvert(char hex[60]);

void hexConvert_from_char(char hex);

void calcPrint(char *argv);

void process_last_3_digits(char *str);

//Polynomial: x^12 + x^11 + x^7 + x^5 + x^4 + x^1 + 1

const char poly[17] = "1100 0101 1001 1";

int main (int argc, char *argv[]) {

//Header print out

printf("CRC Tester by [Your_name]\\n\\n");

fflush(stdout);

  

//Error and exit out if argv are not entered correctly

if(argc != 3) {

printf("ERROR - Entered invlaid number of arguments\\n");

fflush(stdout);

exit(0);

}

  

//Mode type print out

printf("Mode of operation: ");

fflush(stdout);

modeType(argv[1]);

  

//Hex value print out

printf("The input string (hex): %s\\n", argv[2]);

fflush(stdout);

  

//Binary print out of hex values

if (strlen(argv[2]) >= 3 && strlen(argv[2]) < 41) {

printf("The input string (bin): ");

fflush(stdout);

hexConvert(argv[2]);

printf("\\n");

fflush(stdout);

}

else {

printf("ERROR - Invalid hexadecimal length\\n");

exit(0);

}

  

//Print out of polynomial value in binary

printf("\\nThe polynomial that was used (binary bit string): %s\\n", poly);

fflush(stdout);

  

  

//Fifth output if calculation is selected

calcPrint(argv[1]);

  

//Prints out only IF in "verification" mode

if (*argv[1] == \'v\') {

process_last_3_digits(argv[2]);

}

  

  

return 0;

}

//Function slects mode type for first argument

void modeType(char *argv) {

if(*argv == \'v\') {

printf("verification\\n");

fflush(stdout);

}

else if(*argv == \'c\') {

printf("calculation\\n");

fflush(stdout);

}

else {

printf("ERROR - Invalid argument\\n");

fflush(stdout);

exit(0);

}

}

//Function converts hex to binary values

void hexConvert(char hex[60]) {

int i = 0;

while (i < strlen(hex)) {

if (hex[i] == \'0\')

printf("0000 ");

else if (hex[i] == \'1\')

printf("0001 ");

else if (hex[i] == \'2\')

printf("0010 ");

else if (hex[i] == \'3\')

printf("0011 ");

else if (hex[i] == \'4\')

printf("0100 ");

else if (hex[i] == \'5\')

printf("0101 ");

else if (hex[i] == \'6\')

printf("0110 ");

else if (hex[i] == \'7\')

printf("0111 ");

else if (hex[i] == \'8\')

printf("1000 ");

else if (hex[i] == \'9\')

printf("1001 ");

else if (hex[i] == \'A\')

printf("1010 ");

else if (hex[i] == \'B\')

printf("1011 ");

else if (hex[i] == \'C\')

printf("1100 ");

else if (hex[i] == \'D\')

printf("1101 ");

else if (hex[i] == \'E\')

printf("1110 ");

else if (hex[i] == \'F\')

printf("1111 ");

i++;

}

}

//Function converts hex char to binary values, same as above but only for a char

void hexConvert_from_char(char hex) {

if (hex == \'0\')

printf("0000 ");

else if (hex == \'1\')

printf("0001 ");

else if (hex == \'2\')

printf("0010 ");

else if (hex == \'3\')

printf("0011 ");

else if (hex == \'4\')

printf("0100 ");

else if (hex == \'5\')

printf("0101 ");

else if (hex == \'6\')

printf("0110 ");

else if (hex == \'7\')

printf("0111 ");

else if (hex == \'8\')

printf("1000 ");

else if (hex == \'9\')

printf("1001 ");

else if (hex == \'A\')

printf("1010 ");

else if (hex == \'B\')

printf("1011 ");

else if (hex == \'C\')

printf("1100 ");

else if (hex == \'D\')

printf("1101 ");

else if (hex == \'E\')

printf("1110 ");

else if (hex == \'F\')

printf("1111 ");

}

//Function prints out append output in "calculation" mode

void calcPrint(char *argv) {

if (*argv == \'c\') {

printf("Number of zeroes that will be appended to the binary input: 12\\n");

fflush(stdout);

}

}

//Function prints out the last three digits of input string if in "verification" mode

void process_last_3_digits(char *str){

// function to print last line

unsigned long len = strlen(str);

printf("The CRC observed at the end of the input :");

// convert last 3 chars

hexConvert_from_char(str[len - 3]);

hexConvert_from_char(str[len - 2]);

hexConvert_from_char(str[len - 1]);

  

printf(" (bin) = ");

  

printf("%c%c%c (hex) \\n", str[len-3], str[len - 2], str[len - 1] );

}

'

Answer